import React from 'react'
// =====================================================================
import Encrypt from '@com/plugins/encrypt'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== $Lazy
const Page 			= $Lazy.load(()=>import('#tp/page/page-container'))
const Tips 			= $Lazy.load(()=>import('#tp/tips'))
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
const DetailTable 	= $Lazy.state(()=>import('./cpt/detail-table'))
const SingleModal 	= $Lazy.state(()=>import('./cpt/single-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '科室',	name: 'project_name',	type:'select',	loading:true,	idStr:'uuid' },
		],
		formData : [
			{ label: '姓名', 		name:'name', 		full:true,		p:' ',			disabled:true },
			{ label: '用户名',		name: 'account',						full:true,	required:true,	onBlur:(value)=>{this.getName(value)} },
			{ label: '密码',		name: 'password',	type:'password',	full:true,	required:true,	onBlur:(value)=>{this.getName(value)} },
			{ label: '跨岗位领取',	name: 'sub_all',	type:'checkbox' },
		],
	}
	submit = {}
	
	componentDidMount(){
		this.getSelect()
		
		this.fetch()
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 科室
			$fn.setRow( forms, 'project_name', await f.getCache({ api:'ke_shi' }) )
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'receive/pull', { paging: 'get' } )
	
	getName = (value)=>{
		if(value){
			const formValue = this.formRef.getValue(['account','password'])
			$http.submit(null, 'employee/getuserrealname', {param:{phone:formValue.account,password:formValue.password},loading:false }).then((data)=> {
				if(!$fn.hasArray(data)){
					if(data.length === 0){
						this.formRef.setValue({name:''})
					}else{
						this.formRef.setValue({name:data.real_name})
					}
				}
			})
		}
	}
		
	// 表单
	cols = [
		{ title: '标本架代码', 			dataIndex: 'sf_code', 			...$fn.table.spec_code },
		{ title: '可领取的标本编号', 	dataIndex: 'un_receive_count', 	...$fn.table.spec_code },
		{ title: '数量', 				dataIndex: 'spec_count', 		width:100 },
		{ title: '科室', 				dataIndex: 'project_name', 		width:150 },
	]
	
	// 操作按钮
	controls = () => {
		const { selectedKeys, selectedRows} = this.state
		const rows = selectedRows[selectedRows.length-1]
		return [
			Controls.click.call(this, '领取', () => { 	// 领取
				const userData = $fn.getUser()
				const { empl_name, real_name } = userData
				const m = $fn.longSave('remember')
				if($fn.hasObject(m)){
					for(var i in m){ m[i] = Encrypt.decode(m[i]) }
				}
				this.formRef.setValue({account: empl_name, password: m?m.password:'', name:real_name })
				this.modal.open({formRef: this.formRef})
				this.submit.shelf_id = selectedKeys
			}, { isKeys:1, code:'F1'} ),
			Controls.exports.call(this, 'receive/specimenReceiveExport', {team_id:rows? rows.team_id : '',label:'导出外包清单', code:'F2'} ),				// 导出
			Controls.pushTo.call(this, '快捷领取', 'quick-receive' ),	// 快捷领取
			Controls.click.call(this, '单标本领取', () => { 	this.singleModal.open()	},  ),	// 单标本领取
		]
	}
	// 领取
	receive = ()=> {		/* 领取添加异步 2021-8-24 --马婷 */
		const submitData = this.formRef.getValue()
		$http.submit(null,'receive/add',{ param:{...submitData,...this.submit}, loading:true}).then(data=>{
			$fn.msg.success('领取成功')
			this.modal.close()
			this.fetch()
		})
		window.onRefresh = () => {		 
			this.modal.close()
			this.fetch()
		}
	}
	render(){
		return (
			<>
				<Page title='标本领取'  controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{idStr:'uuids',onRow:rows=>{this.detailTableRef.fetch(rows)}})}
					{/* 子表格 */}
					<DetailTable onRef={ ref => this.detailTableRef = ref } />
				</Page>
				{/* 领取 */}
				{FormModal.call(this, {title:'领取', labelWidth:'s6', width:700, api:'',  
					prefix: (<Tips text='请输入用户名与密码进行领取确认' className = 'mb5' />),
					onSubmit: this.receive
				})}
				{/* 单标本领取 */}
				<SingleModal onRef = { ref => this.singleModal = ref }  />
			</>
		)
	}
}

export default Index